// AppbyWizard.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

#define TEMPLAT_PATH ".\\Template"
#define MAXPATH 1024

#define CONFIG_XML "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<olt id=\"%s\" version=\"1.0\" name=\"%s\" defaultlocale=\"ko\">\n\
<language>ko</language>\n\
<language>en-US</language>\n\
<feature name=\"http://wacapps.net/api/deviceapis\" />\n\
<feature name=\"http://wacapps.net/api/accelerometer\" />\n\
<feature name=\"http://wacapps.net/api/deviceinteraction\" />\n\
<feature name=\"http://wacapps.net/api/orientation\" />\n\
<feature name=\"http://wacapps.net/api/devicestatus\" />\n\
<feature name=\"http://wacapps.net/api/camera\" />\n\
<feature name=\"http://wacapps.net/api/pim.contact\" />\n\
<feature name=\"http://wacapps.net/api/filesystem\" />\n\
<feature name=\"http://wacapps.net/api/pim.calendar\" />\n\
<feature name=\"http://wacapps.net/api/pim.task\" />\n\
<feature name=\"http://wacapps.net/api/messaging\" />\n\
<feature name=\"http://wacapps.net/api/accelerometer\" />\n\
<feature name=\"http://wacapps.net/api/accelerometer\" />\n\
<feature name=\"http://www.w3.org/TR/geolocation-API/\" />\n\
</olt>"

#define MAIN_JAVA "package com.appby.%s;\n\
\n\
import com.obigo.webby.webactivity.WebSingleActivity;\n\
\n\
public class L11_WebSingleActivityActivity extends WebSingleActivity {\n\
    private static final String LAUNCHER_CLASSNAME = \"com.obigo.launcher.Launcher\";\n\
    private static final String APP_NAMESPACE = \"com.obigo.l11p\";\n\
\n\
    @Override\n\
    public String getLauncherClassName() {\n\
        return LAUNCHER_CLASSNAME;\n\
    }\n\
\n\
    @Override\n\
    public String getAppNamespace() {\n\
        return APP_NAMESPACE;\n\
    }\n\
}"

#define STRING_XML "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\
<resources>\n\
    <string name=\"app_name\">%s</string>\n\
</resources>"

#define MANIFEST_XML "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\
<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n\
      package=\"com.appby.%s\"\n\
      android:versionCode=\"1\"\n\
      android:versionName=\"1.0\">\n\
    <uses-sdk android:minSdkVersion=\"8\" />\n\
\n\
    <uses-permission android:name=\"android.permission.CALL_PHONE\" />\n\
    <uses-permission android:name=\"android.permission.EXPAND_STATUS_BAR\" />\n\
    <uses-permission android:name=\"android.permission.GET_TASKS\" />\n\
    <uses-permission android:name=\"android.permission.READ_CONTACTS\"/>\n\
    <uses-permission android:name=\"android.permission.WRITE_CONTACTS\"/>\n\
    <uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\"/>\n\
    <uses-permission android:name=\"android.permission.SET_WALLPAPER\" />\n\
    <uses-permission android:name=\"android.permission.SET_WALLPAPER_HINTS\" />\n\
    <uses-permission android:name=\"android.permission.VIBRATE\" />\n\
    <uses-permission android:name=\"android.permission.WRITE_SETTINGS\" />\n\
    <uses-permission android:name=\"android.permission.BIND_APPWIDGET\" />\n\
    <uses-permission android:name=\"com.android.launcher.permission.READ_SETTINGS\" />\n\
    <uses-permission android:name=\"com.android.launcher.permission.WRITE_SETTINGS\" />\n\
    <uses-permission android:name=\"android.permission.INTERNET\"/>\n\
    <uses-permission android:name=\"android.permission.READ_CALENDAR\" />\n\
    <uses-permission android:name=\"android.permission.WRITE_CALENDAR\" />\n\
    <uses-permission android:name=\"com.google.android.googleapps.permission.GOOGLE_AUTH\" />\n\
    <uses-permission android:name=\"com.google.android.googleapps.permission.GOOGLE_AUTH.cl\" />\n\
    <uses-permission android:name=\"com.google.android.googleapps.permission.GOOGLE_AUTH.mail\" />\n\
    <uses-permission android:name=\"android.permission.GET_ACCOUNTS\" />\n\
    <uses-permission android:name=\"android.permission.READ_PHONE_STATE\" />\n\
    <uses-permission android:name=\"android.permission.WRITE_SECURE_SETTINGS\" />\n\
    <uses-permission android:name=\"android.permission.BATTERY_STATS\" />\n\
    <uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\" />\n\
    <uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\" />\n\
    <uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" />\n\
    <uses-permission android:name=\"android.permission.INSTALL_LOCATION_PROVIDER\" />\n\
    <uses-permission android:name=\"android.permission.BLUETOOTH_ADMIN\" />\n\
    <uses-permission android:name=\"android.permission.BLUETOOTH\" />\n\
    <uses-permission android:name=\"android.permission.READ_SMS\" />\n\
    <uses-permission android:name=\"android.permission.SEND_SMS\" />\n\
    <uses-permission android:name=\"android.permission.WRITE_SMS\" />\n\
    <uses-permission android:name=\"android.permission.RECEIVE_SMS\" />\n\
    <uses-permission android:name=\"android.permission.CAMERA\"/>\n\
    <uses-permission android:name=\"android.permission.RECORD_AUDIO\"/>\n\
    <uses-permission android:name=\"android.permission.RECORD_VIDEO\"/>\n\
    <uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>\n\
    <uses-permission android:name=\"com.android.email.permission.ACCESS_PROVIDER\"/>\n\
    <uses-permission android:name=\"android.permission.MODIFY_AUDIO_SETTINGS\" />\n\
    <uses-permission android:name=\"android.permission.INSTALL_PACKAGES\" />\n\
    <uses-permission android:name=\"android.permission.WAKE_LOCK\" />\n\
    <uses-permission android:name=\"android.permission.DUMP\" />\n\
    <uses-permission android:name=\"android.permission.DEVICE_POWER\"/>\n\
    <uses-permission android:name=\"android.permission.DISABLE_KEYGUARD\"/>\n\
    <uses-permission android:name=\"android.permission.CHANGE_CONFIGURATION\"/>\n\
\n\
    <application android:icon=\"@drawable/icon\" android:label=\"@string/app_name\">\n\
        <activity android:name=\".L11_WebSingleActivityActivity\"\n\
                  android:theme=\"@android:style/Theme.NoTitleBar\"\n\
                  android:screenOrientation=\"nosensor\"\n\
                  android:label=\"@string/app_name\">\n\
            <intent-filter>\n\
                <action android:name=\"android.intent.action.MAIN\" />\n\
                <category android:name=\"android.intent.category.LAUNCHER\" />\n\
            </intent-filter>\n\
        </activity>\n\
\n\
    </application>\n\
</manifest>"



int _tmain(int argc, _TCHAR* argv[])
{
	if (argc < 2 || (argc == 2 && strcmp(argv[1], "--help") == 0))
	{
		cout << "[HELP] AppbyWizard [Project Name] [Widget ID]" << endl;
		return 0;
	}

	cout << "====================================" << endl;
	for (int i=0; i<argc; ++i)
	{
		cout << argv[i] << endl;
	}

	string packageName = argv[1];
	string projectName = argv[1];
	string widgetID = argv[2];
	
	for (int i=0; i<packageName.size(); ++i)
	{
		packageName[i] = tolower(packageName[i]);
	}

	cout << "====================================" << endl;

	// 1 = class name
	char path[MAXPATH] = {0};
	char targetPath[MAXPATH] = {0};

	sprintf(targetPath, "..\\appby\\%s", projectName.c_str());
	sprintf(path, "xcopy .\\%s\\*.* %s /e /h /i", TEMPLAT_PATH, targetPath);
	cout << "Copyfiles : " << path << endl;
	system(path);
	
	char buffer[8192] = {0};
	std::string fileName;
	FILE* fp = NULL;

	// AndroidManifest.xml
	fileName = targetPath;
	fileName += "\\AndroidManifest.xml";
	cout << "Makefile : " << fileName.c_str() << endl;
	sprintf(buffer, MANIFEST_XML, packageName.c_str());
	fp = fopen(fileName.c_str(), "w");
	fputs(buffer, fp);
	fclose(fp);

	cout << "write ok" << endl;

	// asset/webbyview/config.xml
	fileName = targetPath;
	fileName += "\\assets\\webbyview\\config.xml";
	cout << "Makefile : " << fileName.c_str() << endl;
	sprintf(buffer, CONFIG_XML, widgetID.c_str(), projectName.c_str());
	fp = fopen(fileName.c_str(), "w");
	fputs(buffer, fp);
	fclose(fp);

	cout << "write ok" << endl;

	// res/values/strings.xml
	fileName = targetPath;
	fileName += "\\res\\values\\strings.xml";
	cout << "Makefile : " << fileName.c_str() << endl;
	sprintf(buffer, STRING_XML, projectName.c_str());
	fp = fopen(fileName.c_str(), "w");
	fputs(buffer, fp);
	fclose(fp);

	cout << "write ok" << endl;

	// src/com/appby/$packageName/L11_WebSingleActivityActivity.java
	fileName = targetPath;
	fileName += "\\src\\com\\appby\\" + packageName;

	string makeDir = "mkdir " + fileName;
	system(makeDir.c_str());

	fileName += "\\L11_WebSingleActivityActivity.java";
	cout << "Makefile : " << fileName.c_str() << endl;
	sprintf(buffer, MAIN_JAVA, packageName.c_str());
	fp = fopen(fileName.c_str(), "w");
	fputs(buffer, fp);
	fclose(fp);

	cout << "write ok" << endl;

	return 1;
}

